package com.uchengwang.ui.cm_ui.ucity.presenter;

import android.app.Activity;
import android.text.TextUtils;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.ThemeConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.listener.IHttpManager;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.functions.http.listener.OnHttpResultSingleListener;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.ThemeBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.ThemeListShareBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.ThemeNoticeBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.ThemeNoticeListBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.ThemeShareBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.UserBeanList;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/11/27.
 */

public class HttpThemeManager {

    private static HttpThemeManager manager;
    private HttpThemeManager (){}
    private boolean isAllow = true;

    public static HttpThemeManager getManager() {
        if (manager == null){
            manager = new HttpThemeManager();
        }
        return manager;
    }

    /**
     * 获取某个学校的话题
     * */
    public void getTheme(Activity activity, final OnHttpResultListener<ThemeBean> listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.UNI_ID, UChengApplication.uniId);
        HttpManager.newInstance(activity).queryCheck(ThemeConfig.Theme.method_get, ThemeConfig.Theme.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                ThemeBean clubBean = new ThemeBean();
                clubBean.setBannerTitle("社团纳新");
                clubBean.setResId(R.drawable.icon_naxin);

                ThemeBean help = new ThemeBean();
                help.setBannerTitle("校园帮问");
                help.setResId(R.drawable.icon_bangwen);
                List<ThemeBean> list = new ArrayList<ThemeBean>();

                list.add(clubBean);
                list.add(help);

                listener.onResult(status, msg, list.size(), list);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                List<ThemeBean> list = null;
                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    list = JsonUtils.getManager().parseList(data, ThemeBean.class);
                }
                if (list == null){
                    list = new ArrayList<ThemeBean>();
                }
                int size = list.size();
                if (size <2){
                    ThemeBean clubBean = new ThemeBean();
                    clubBean.setBannerTitle("社团纳新");
                    clubBean.setResId(R.drawable.icon_naxin);

                    ThemeBean help = new ThemeBean();
                    help.setBannerTitle("校园帮问");
                    help.setResId(R.drawable.icon_bangwen);

                    switch (size){
                        case 0:
                            list.add(clubBean);
                            list.add(help);
                            break;
                        case 1:
                            list.add(clubBean);
                            break;
                    }
                }
                listener.onResult(status, msg, list.size(), list);

            }
        });
    }

    /**
     * 获取某个banner的所有发布信息
     * */
    public void getThemeShares(Activity activity, long themeId, int page, final OnHttpResultListener<ThemeShareBean> listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.TOPIC_ID, themeId);
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, 10);
        HttpManager.newInstance(activity).query(ThemeConfig.ThemeShare.method_get, ThemeConfig.ThemeShare.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status,msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    ThemeListShareBean bean = JsonUtils.getManager().stringToBean(data, ThemeListShareBean.class);
                    listener.onResult(status, msg, bean.getTotalPage(), bean.getData());
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });
    }

    /**
     * 获取某个话题的某一条发布信息
     * */
    public void getThemeShare(Activity activity, long shareId, final OnHttpResultSingleListener<ThemeShareBean> listener){
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.SHARE_ID, shareId);
        HttpManager.newInstance(activity).query(ThemeConfig.ThemeShare.method_get, ThemeConfig.ThemeShare.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                listener.onResult(status, msg, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                if (status ==0 && !TextUtils.isEmpty(data)){
                    ThemeShareBean bean = JsonUtils.getManager().stringToBean(data, ThemeShareBean.class);
                    listener.onResult(status, msg, bean);
                }else {
                    listener.onResult(status, msg, null);
                }
            }
        });


    }

    /**
     * 获取点赞享说人数
     * */
    public void getThemeLike(Activity activity, final long shareId, int page, int perPage, final OnHttpResultListener<UserBean> listener){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.SHARE_ID, shareId);
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, perPage);
        HttpManager.newInstance(activity).query(ThemeConfig.ThemeLike.method_get, ThemeConfig.ThemeLike.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    UserBeanList bean = JsonUtils.getManager().stringToBean(data, UserBeanList.class);
                    listener.onResult(status, bean.getTotal()+"", bean.getTotalPage(), bean.getData());
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });

    }



    /**
     * 某个话题里 某条信息的点赞
     * @param zan 当前状态是已赞过 还是未赞
     * */
    public void like(final Activity activity, boolean zan, long shareId, final OnResultListener listener){
        if (UCommonUtil.getUtil().isNeedToLogin(activity, false)){
            return;
        }
        if (!isAllow){
            //上次的网络请求还没返回 禁止再次请求
            return;
        }
        LoadingUtils.getUtils().loading(activity);

        final IHttpManager.RequestMethod method = zan ? ThemeConfig.ThemeLike.method_delete : ThemeConfig.ThemeLike.method_post;
        MapRequest mapRequest = new MapRequest();
        mapRequest.put(BaseConfig.TOPIC_SHARE_ID, shareId);
        HttpManager.newInstance(activity).query(method, ThemeConfig.ThemeLike.URL, mapRequest, new HttpCallBack() {
            @Override
            public void onError(int code, String msg) {
                isAllow = true;
                LoadingUtils.getUtils().hide();
                UCommonUtil.getUtil().showToast(activity, "操作失败");
            }
            @Override
            public void onResp(int code, String msg, String data) {
                isAllow = true;
                LoadingUtils.getUtils().hide();
                listener.onResult(code, msg, 0, null);

            }
        });
        isAllow = false;
    }

    /**
     * 删除话题里的某一个发布信息
     * @param uid 发布人ID
     * @param shareId
     * */

    public void delete(Activity activity, long uid, final long shareId, String banReason, final OnResultListener listener){
        if (!isAllow){
            //上次的网络请求还没返回 禁止再次请求
            return;
        }
        LoadingUtils.getUtils().loading(activity);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.SHARE_ID, shareId);


        IHttpManager.RequestMethod method = null;
        String url = "";
        if (uid == UCommonUtil.getUtil().getId()){
            //自己发布的 自己删除
            method = ThemeConfig.ThemeShare.method_delete;
            url = ThemeConfig.ThemeShare.URL;
        }else if (UChengApplication.userRole ==BaseConfig.UNI_ROLE || UChengApplication.userRole == BaseConfig.ZONG_ROLE){
            method = ThemeConfig.ThemeUniShare.method;
            url = ThemeConfig.ThemeUniShare.URL;

            if (!TextUtils.isEmpty(banReason)){
                map.put(BaseConfig.BAN_REASON, banReason);
            }
        }

        if (method == null || TextUtils.isEmpty(url)){
            return;
        }

        HttpManager.newInstance(activity).query(method, url, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                LoadingUtils.getUtils().hide();
                isAllow = true;
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                LoadingUtils.getUtils().hide();
                isAllow = true;
                listener.onResult(status, msg, 0, null);
            }
        });
        isAllow = false;
    }

    /**
     * 获取话题的互动消息
     * @param themeId 话题的ID,如果不传 则获取所有的未读
     * @param isRead 是否已读,不传则默认获取所有
     * @param updateRead 如果该字段为true,当调用该接口后,被获取的数据就会被标记为已读
     * */
    public void getThemeNotices(Activity activity, long themeId, Boolean isRead, boolean updateRead, int page, int perPage, final OnHttpResultListener<ThemeNoticeBean> listener){
        MapRequest map = new MapRequest();
        if (themeId >0){
            map.put(BaseConfig.TOPIC_ID, themeId);
        }
        if (isRead != null){
            map.put(BaseConfig.IS_READ, isRead);
        }
        map.put(BaseConfig.UPDATE_READ, updateRead);
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, perPage);
        HttpManager.newInstance(activity).query(ThemeConfig.ThemeNotice.method, ThemeConfig.ThemeNotice.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                listener.onResult(status, msg, 0, null);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                if (status ==0 && !TextUtils.isEmpty(data) && !data.equals("[]")){
                    ThemeNoticeListBean bean = JsonUtils.getManager().stringToBean(data, ThemeNoticeListBean.class);
                    listener.onResult(status, msg, bean.getTotalPage(), bean.getData());
                }else {
                    listener.onResult(status, msg, 0, null);
                }
            }
        });


    }


}
